<!DOCTYPE html>
<html lang=zh>
<head>
    <meta charset="utf-8">
    
    <title>Java数据类型 | 紫焱</title>
    
    
        <meta name="keywords" content="语法规则" />
    
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
    <meta name="description" content="当创建变量的时候，内存管理系统会根据变量的数据类型未变量分配存储空间，而在Java中的数据类型有两种：基本数据类型(又称内置数据类型)和引用数据类型。 基本数据类型Java的数据类型有八种，四个整数型，两个浮点数型，一个字符型，一个布尔型。根据不同的数据类型的特点进行了下表的总结：    基本数据类型 位数 最小值 最大值 默认值     byte 8 -128（-2^7） 127（2^7-1）">
<meta name="keywords" content="语法规则">
<meta property="og:type" content="article">
<meta property="og:title" content="Java数据类型">
<meta property="og:url" content="http://yoursite.com/2018/08/12/Java数据类型/index.html">
<meta property="og:site_name" content="紫焱">
<meta property="og:description" content="当创建变量的时候，内存管理系统会根据变量的数据类型未变量分配存储空间，而在Java中的数据类型有两种：基本数据类型(又称内置数据类型)和引用数据类型。 基本数据类型Java的数据类型有八种，四个整数型，两个浮点数型，一个字符型，一个布尔型。根据不同的数据类型的特点进行了下表的总结：    基本数据类型 位数 最小值 最大值 默认值     byte 8 -128（-2^7） 127（2^7-1）">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2018-09-07T12:10:30.653Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Java数据类型">
<meta name="twitter:description" content="当创建变量的时候，内存管理系统会根据变量的数据类型未变量分配存储空间，而在Java中的数据类型有两种：基本数据类型(又称内置数据类型)和引用数据类型。 基本数据类型Java的数据类型有八种，四个整数型，两个浮点数型，一个字符型，一个布尔型。根据不同的数据类型的特点进行了下表的总结：    基本数据类型 位数 最小值 最大值 默认值     byte 8 -128（-2^7） 127（2^7-1）">
    

    
        <link rel="alternate" href="/atom.xml" title="紫焱" type="application/atom+xml" />
    

    
        <link rel="icon" href="/favicon.ico" />
    

    <link rel="stylesheet" href="/libs/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="/libs/open-sans/styles.css">
    <link rel="stylesheet" href="/libs/source-code-pro/styles.css">

    <link rel="stylesheet" href="/css/style.css">
    <script src="/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="/libs/jquery/plugins/cookie/1.4.1/jquery.cookie.js"></script>
    
    
        <link rel="stylesheet" href="/libs/lightgallery/css/lightgallery.min.css">
    
    
        <link rel="stylesheet" href="/libs/justified-gallery/justifiedGallery.min.css">
    
    
    
    


    
        <script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    
</head>

<body>
    <div id="container">
        <header id="header">
    <div id="header-main" class="header-inner">
        <div class="outer">
            <a href="/" id="logo">
                <i class="logo"></i>
                <span class="site-title">紫焱</span>
            </a>
            <nav id="main-nav">
                
                    <a class="main-nav-link" href="/">首页</a>
                
                    <a class="main-nav-link" href="/archives">归档</a>
                
                    <a class="main-nav-link" href="/categories">分类</a>
                
                    <a class="main-nav-link" href="/tags">标签</a>
                
                    <a class="main-nav-link" href="/about">关于</a>
                
            </nav>
            
            <div id="search-form-wrap">

    <form class="search-form">
        <input type="text" class="ins-search-input search-form-input" placeholder="搜索" />
        <button type="submit" class="search-form-submit"></button>
    </form>
    <div class="ins-search">
    <div class="ins-search-mask"></div>
    <div class="ins-search-container">
        <div class="ins-input-wrapper">
            <input type="text" class="ins-search-input" placeholder="想要查找什么..." />
            <span class="ins-close ins-selectable"><i class="fa fa-times-circle"></i></span>
        </div>
        <div class="ins-section-wrapper">
            <div class="ins-section-container"></div>
        </div>
    </div>
</div>
<script>
(function (window) {
    var INSIGHT_CONFIG = {
        TRANSLATION: {
            POSTS: '文章',
            PAGES: '页面',
            CATEGORIES: '分类',
            TAGS: '标签',
            UNTITLED: '(未命名)',
        },
        ROOT_URL: '/',
        CONTENT_URL: '/content.json',
    };
    window.INSIGHT_CONFIG = INSIGHT_CONFIG;
})(window);
</script>
<script src="/js/insight.js"></script>

</div>
        </div>
    </div>
    <div id="main-nav-mobile" class="header-sub header-inner">
        <table class="menu outer">
            <tr>
                
                    <td><a class="main-nav-link" href="/">首页</a></td>
                
                    <td><a class="main-nav-link" href="/archives">归档</a></td>
                
                    <td><a class="main-nav-link" href="/categories">分类</a></td>
                
                    <td><a class="main-nav-link" href="/tags">标签</a></td>
                
                    <td><a class="main-nav-link" href="/about">关于</a></td>
                
                <td>
                    
    <div class="search-form">
        <input type="text" class="ins-search-input search-form-input" placeholder="搜索" />
    </div>

                </td>
            </tr>
        </table>
    </div>
</header>

        <div class="outer">
            
            
                <aside id="sidebar">
   
        
    <div class="widget-wrap" id='categories'>
        <h3 class="widget-title">
            <span>分类</span>
            &nbsp;
            <a id='allExpand' href="#">
                <i class="fa fa-angle-double-down fa-2x"></i>
            </a>
        </h3>
        
        
        
         <ul class="unstyled" id="tree" > 
                    <li class="directory open">
                        <a href="#" data-role="directory">
                            <i class="fa fa-folder-open"></i>
                            &nbsp;
                            Java
                        </a>
                         <ul class="unstyled" id="tree" > 
                    <li class="directory open">
                        <a href="#" data-role="directory">
                            <i class="fa fa-folder-open"></i>
                            &nbsp;
                            基础语法
                        </a>
                         <ul class="unstyled" id="tree" >  <li class="file"><a href="/2018/08/12/Java安装/">Java环境安装</a></li>  <li class="file active"><a href="/2018/08/12/Java数据类型/">Java数据类型</a></li>  <li class="file"><a href="/2018/08/12/Java修饰符/">Java修饰符</a></li>  <li class="file"><a href="/2018/08/12/Java命名注释/">Java命名与注释</a></li>  <li class="file"><a href="/2018/08/12/Java运算符/">Java运算符</a></li>  <li class="file"><a href="/2018/09/07/Java语句/">Java语句</a></li>  </ul> 
                    </li> 
                     </ul> 
                    </li> 
                    
                    <li class="directory">
                        <a href="#" data-role="directory">
                            <i class="fa fa-folder"></i>
                            &nbsp;
                            git
                        </a>
                         <ul class="unstyled" id="tree" >  <li class="file"><a href="/2018/12/01/git操作命令/">git操作命令</a></li>  </ul> 
                    </li> 
                     <li class="file"><a href="/2018/08/11/index/">index</a></li>  </ul> 
    </div>
    <script>
        $(document).ready(function() {
            var iconFolderOpenClass  = 'fa-folder-open';
            var iconFolderCloseClass = 'fa-folder';
            var iconAllExpandClass = 'fa-angle-double-down';
            var iconAllPackClass = 'fa-angle-double-up';
            // Handle directory-tree expansion:
            // 左键单独展开目录
            $(document).on('click', '#categories a[data-role="directory"]', function (event) {
                event.preventDefault();

                var icon = $(this).children('.fa');
                var expanded = icon.hasClass(iconFolderOpenClass);
                var subtree = $(this).siblings('ul');
                icon.removeClass(iconFolderOpenClass).removeClass(iconFolderCloseClass);
                if (expanded) {
                    if (typeof subtree != 'undefined') {
                        subtree.slideUp({ duration: 100 });
                    }
                    icon.addClass(iconFolderCloseClass);
                } else {
                    if (typeof subtree != 'undefined') {
                        subtree.slideDown({ duration: 100 });
                    }
                    icon.addClass(iconFolderOpenClass);
                }
            });
            // 右键展开下属所有目录
            $('#categories a[data-role="directory"]').bind("contextmenu", function(event){
                event.preventDefault();
                
                var icon = $(this).children('.fa');
                var expanded = icon.hasClass(iconFolderOpenClass);
                var listNode = $(this).siblings('ul');
                var subtrees = $.merge(listNode.find('li ul'), listNode);
                var icons = $.merge(listNode.find('.fa'), icon);
                icons.removeClass(iconFolderOpenClass).removeClass(iconFolderCloseClass);
                if(expanded) {
                    subtrees.slideUp({ duration: 100 });
                    icons.addClass(iconFolderCloseClass);
                } else {
                    subtrees.slideDown({ duration: 100 });
                    icons.addClass(iconFolderOpenClass);
                }
            })
            // 展开关闭所有目录按钮
            $(document).on('click', '#allExpand', function (event) {
                event.preventDefault();
                
                var icon = $(this).children('.fa');
                var expanded = icon.hasClass(iconAllExpandClass);
                icon.removeClass(iconAllExpandClass).removeClass(iconAllPackClass);
                if(expanded) {
                    $('#sidebar .fa.fa-folder').removeClass('fa-folder').addClass('fa-folder-open')
                    $('#categories li ul').slideDown({ duration: 100 });
                    icon.addClass(iconAllPackClass);
                } else {
                    $('#sidebar .fa.fa-folder-open').removeClass('fa-folder-open').addClass('fa-folder')
                    $('#categories li ul').slideUp({ duration: 100 });
                    icon.addClass(iconAllExpandClass);
                }
            });  
        });
    </script>

    
    <div id="toTop" class="fa fa-angle-up"></div>
</aside>
            
            <section id="main"><article id="post-Java数据类型" class="article article-type-post" itemscope itemprop="blogPost">
    <div class="article-inner">
        
        
            <header class="article-header">
                
                    <div class="article-meta">
                        
    <div class="article-category">
    	<i class="fa fa-folder"></i>
        <a class="article-category-link" href="/categories/Java/">Java</a><i class="fa fa-angle-right"></i><a class="article-category-link" href="/categories/Java/基础语法/">基础语法</a>
    </div>

                        
    <div class="article-tag">
        <i class="fa fa-tag"></i>
        <a class="tag-link" href="/tags/语法规则/">语法规则</a>
    </div>

                        
    <div class="article-date">
        <i class="fa fa-calendar"></i>
        <a href="/2018/08/12/Java数据类型/">
            <time datetime="2018-08-12T00:30:19.000Z" itemprop="datePublished">2018-08-12</time>
        </a>
    </div>


                        
                            <i class="fa fa-bar-chart"></i>
                            <span id="busuanzi_container_site_pv"><span id="busuanzi_value_page_pv"></span></span>    
                        
                        
                            <div class="article-meta-button">
                                <a href='https://gitee.com/liuyi6/liuyi6/raw/hexo/source/_posts/Java数据类型.md'> Source </a>
                            </div>
                            <div class="article-meta-button">
                                <a href='https://gitee.com/liuyi6/liuyi6/edit/hexo/source/_posts/Java数据类型.md'> Edit </a>
                            </div>
                            <div class="article-meta-button">
                                <a href='https://gitee.com/liuyi6/liuyi6/commits/hexo/source/_posts/Java数据类型.md'> History </a>
                            </div>
                        
                    </div>
                
                
    
        <h1 class="article-title" itemprop="name">
            Java数据类型
        </h1>
    

            </header>
        
        
        <div class="article-entry" itemprop="articleBody">
        
        
            
        
        
            <p>当创建变量的时候，内存管理系统会根据变量的数据类型未变量分配存储空间，而在Java中的数据类型有两种：基本数据类型(又称内置数据类型)和引用数据类型。</p>
<h2 id="基本数据类型"><a href="#基本数据类型" class="headerlink" title="基本数据类型"></a>基本数据类型</h2><p>Java的数据类型有八种，四个整数型，两个浮点数型，一个字符型，一个布尔型。<br>根据不同的数据类型的特点进行了下表的总结：</p>
<table>
<thead>
<tr>
<th>基本数据类型</th>
<th>位数</th>
<th>最小值</th>
<th>最大值</th>
<th>默认值</th>
</tr>
</thead>
<tbody>
<tr>
<td>byte</td>
<td>8</td>
<td>-128（-2^7）</td>
<td>127（2^7-1）</td>
<td>(byte)0</td>
</tr>
<tr>
<td>short</td>
<td>16</td>
<td>-32768（-2^15）</td>
<td>32767（2^15 - 1）</td>
<td>(short)0</td>
</tr>
<tr>
<td>int</td>
<td>32</td>
<td>-2,147,483,648（-2^31）</td>
<td>2,147,483,647（2^31 - 1）</td>
<td>0</td>
</tr>
<tr>
<td>long</td>
<td>64</td>
<td>-9,223,372,036,854,775,808（-2^63）</td>
<td>9,223,372,036,854,775,807（2^63 -1）</td>
<td>0L</td>
</tr>
<tr>
<td>float</td>
<td>32</td>
<td>-3.4E38</td>
<td>3.4E8</td>
<td>0.0f</td>
</tr>
<tr>
<td>double</td>
<td>64</td>
<td>-1.7E08</td>
<td>1.7E08</td>
<td>0.0d</td>
</tr>
<tr>
<td>char</td>
<td>16</td>
<td>\u0000 (0)</td>
<td>\uffff (65535)</td>
<td>‘\uoooo’ (null)</td>
</tr>
<tr>
<td>boolean</td>
<td>8</td>
<td>fasle</td>
<td>true</td>
<td>false</td>
</tr>
</tbody>
</table>
<p>注意：<br>Float和Double的最小值和最大值都是以科学记数法的形式输出的，结尾的”E+数字”表示E之前的数字要乘以10的多少次方。比如3.14E3就是3140，3.14E-3 就是 0.00314。<br>基本数据类型的具体应用代码如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line">public class NumberTest&#123;</span><br><span class="line">     public static void main(String []args)&#123;</span><br><span class="line">        System.out.println(&quot;byte的位数：&quot; + Byte.SIZE); </span><br><span class="line">        System.out.println(&quot;最小值：&quot; + Byte.MIN_VALUE);  </span><br><span class="line">        System.out.println(&quot;最大值：&quot; + Byte.MAX_VALUE);</span><br><span class="line">        System.out.println(&quot;--------------------------------&quot;);</span><br><span class="line">        System.out.println(&quot;short的位数：&quot; + Short.SIZE); </span><br><span class="line">        System.out.println(&quot;最小值：&quot; + Short.MIN_VALUE);  </span><br><span class="line">        System.out.println(&quot;最大值：&quot; + Short.MAX_VALUE);</span><br><span class="line">        System.out.println(&quot;--------------------------------&quot;);</span><br><span class="line">        System.out.println(&quot;int的位数：&quot; + Integer.SIZE); </span><br><span class="line">        System.out.println(&quot;最小值：&quot; + Integer.MIN_VALUE);  </span><br><span class="line">        System.out.println(&quot;最大值：&quot; + Integer.MAX_VALUE);</span><br><span class="line">        System.out.println(&quot;--------------------------------&quot;);</span><br><span class="line">        System.out.println(&quot;long的位数：&quot; + Long.SIZE); </span><br><span class="line">        System.out.println(&quot;最小值：&quot; + Long.MIN_VALUE);  </span><br><span class="line">        System.out.println(&quot;最大值：&quot; + Long.MAX_VALUE);</span><br><span class="line">        System.out.println(&quot;--------------------------------&quot;);</span><br><span class="line">        System.out.println(&quot;float的位数：&quot; + Float.SIZE); </span><br><span class="line">        System.out.println(&quot;最小值：&quot; + Float.MIN_VALUE);  </span><br><span class="line">        System.out.println(&quot;最大值：&quot; + Float.MAX_VALUE);</span><br><span class="line">        System.out.println(&quot;--------------------------------&quot;);</span><br><span class="line">        System.out.println(&quot;double的位数：&quot; + Double.SIZE); </span><br><span class="line">        System.out.println(&quot;最小值：&quot; + Double.MIN_VALUE);  </span><br><span class="line">        System.out.println(&quot;最大值：&quot; + Double.MAX_VALUE);</span><br><span class="line">        System.out.println(&quot;--------------------------------&quot;);</span><br><span class="line">        System.out.println(&quot;char的位数：&quot; + Character.SIZE); </span><br><span class="line">        System.out.println(&quot;最小值：&quot; + (int)Character.MIN_VALUE);  </span><br><span class="line">        System.out.println(&quot;最大值：&quot; + (int)Character.MAX_VALUE);</span><br><span class="line">     &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>运行结果如下：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line">byte的位数：8</span><br><span class="line">最小值：-128</span><br><span class="line">最大值：127</span><br><span class="line">--------------------------------</span><br><span class="line">short的位数：16</span><br><span class="line">最小值：-32768</span><br><span class="line">最大值：32767</span><br><span class="line">--------------------------------</span><br><span class="line">int的位数：32</span><br><span class="line">最小值：-2147483648</span><br><span class="line">最大值：2147483647</span><br><span class="line">--------------------------------</span><br><span class="line">long的位数：64</span><br><span class="line">最小值：-9223372036854775808</span><br><span class="line">最大值：9223372036854775807</span><br><span class="line">--------------------------------</span><br><span class="line">float的位数：32</span><br><span class="line">最小值：1.4E-45</span><br><span class="line">最大值：3.4028235E38</span><br><span class="line">--------------------------------</span><br><span class="line">double的位数：64</span><br><span class="line">最小值：4.9E-324</span><br><span class="line">最大值：1.7976931348623157E308</span><br><span class="line">--------------------------------</span><br><span class="line">char的位数：16</span><br><span class="line">最小值：0</span><br><span class="line">最大值：65535</span><br></pre></td></tr></table></figure>
<h2 id="引用类型"><a href="#引用类型" class="headerlink" title="引用类型"></a>引用类型</h2><p>在Java中，引用类型指向一个对象，指向对象的变量是引用变量。这些变量在声明时被指定为一个特定的类型。变量一旦声明后，类型就不能被改变了。它的值是指向内存空间的引用，就是地址。<br>Java的引用类型包括：类，接口，数组</p>
<h2 id="基本数据类型与引用类型的区别"><a href="#基本数据类型与引用类型的区别" class="headerlink" title="基本数据类型与引用类型的区别"></a>基本数据类型与引用类型的区别</h2><table>
<thead>
<tr>
<th>项目</th>
<th>基本数据类型</th>
<th>引用数据类型</th>
</tr>
</thead>
<tbody>
<tr>
<td>概念</td>
<td>变量名指向具体数值</td>
<td>变量名指向内存数据对象的内存地址</td>
</tr>
<tr>
<td>内存构建</td>
<td>声明之后立刻分配内存空间</td>
<td>存储了一个内存地址</td>
</tr>
<tr>
<td>内存分配</td>
<td>变量存储在栈内存中</td>
<td>变量在栈内存中保存的是对象在堆内存中的地址</td>
</tr>
<tr>
<td>赋值</td>
<td>需要赋具体值</td>
<td>可以赋null</td>
</tr>
</tbody>
</table>
<p>注意事项：</p>
<ul>
<li><p>java开发过程中整型用int、小数用double、布尔用boolean；</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">(1)int a=12;</span><br><span class="line">double b=a;</span><br><span class="line">(2)double a=3.0;</span><br><span class="line">int b=(int)a;</span><br></pre></td></tr></table></figure>
</li>
<li><p>long,byte,char等很少用到，一般在数据处理的时候会用到；</p>
</li>
<li><p>int跟char之间的转换是通过ASCII转化的；</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">char a =&apos;A&apos;;</span><br><span class="line">int b=a;</span><br><span class="line">System.out.prientln(b);</span><br></pre></td></tr></table></figure>
</li>
<li><p>String类型，String是项目中必不可少且使用最多的数据类型，它属于引用数据类型中“类”的范畴。</p>
</li>
</ul>
<h2 id="类型转换"><a href="#类型转换" class="headerlink" title="类型转换"></a>类型转换</h2><p>数据类型转换必须满足如下规则：</p>
<ol>
<li>不能对boolean类型进行类型转换。</li>
<li>不能把对象类型转换成不相关类的对象。</li>
<li>在把容量大的类型转换为容量小的类型时必须使用强制类型转换。</li>
<li>转换过程中可能导致溢出或损失精度。</li>
<li>因为 byte 类型是 8 位，最大值为127，所以当 int 强制转换为 byte 类型时，值 128 时候就会导致溢出。</li>
</ol>
<p>浮点数到整数的转换是通过舍弃小数得到，而不是四舍五入。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">int i =128;   </span><br><span class="line">byte b = (byte)i;</span><br></pre></td></tr></table></figure></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">(int)23.7 == 23;        </span><br><span class="line">(int)-45.89f == -45</span><br></pre></td></tr></table></figure>
<p><strong>自动类型转换</strong><br>整型、实型（常量）、字符型数据可以混合运算。运算中，不同类型的数据先转化为同一类型，然后进行运算。<br>转换从低级到高级。<br>必须满足转换前的数据类型的位数要低于转换后的数据类型，例如：<br>short数据类型的位数为16位，就可以自动转换位数为32的int类型。<br>float数据类型的位数为32，可以自动转换为64位的double类型。<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">低  ------------------------------------&gt;  高</span><br><span class="line">byte,short,char—&gt; int —&gt; long—&gt; float —&gt; double</span><br></pre></td></tr></table></figure></p>
<p>例1：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">public static void main(String[] args) &#123;</span><br><span class="line">			byte b = 4;</span><br><span class="line">			int  x = 3;</span><br><span class="line">			x = x + b;</span><br><span class="line">			System.out.println(&quot;x=&quot;+x);	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>运行结果：7<br>在Java中不同的数据类型是不能进行运算的，但在上例中却可以进行编译，并输出运行结果7。<br>这是因为在Java中的运算特性：自动类型转换（以大的为主，小的向大的提升b(byte)–&gt;x(int)）<br>在上述运算中发生：x(int) = b(byte) + x(int) –&gt; x(int) = b(int) + x(int) ，运算时b就会进行自动类型提升<br>类型提升之后，x和b就可以进行运算，运算之后的结果为7。<br>例2：</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">public static void main(String[] args) &#123;</span><br><span class="line">			byte b = 3;</span><br><span class="line">			b = b + 1;</span><br><span class="line">			System.out.println(&quot;b=&quot;+b);	</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>运行结果：编译错误<br>与例一原理相同，b作为一个byte类型变量在与1（默认int类型变量）进行运算时会进行自动类型提升，最终算出的结果是int类型的，而等式左边的b是一个byte类型的变量，所以会报错。</p>
<p>详细的相关解释见：<a href="https://blog.csdn.net/a909301740/article/details/52084853" target="_blank" rel="noopener">JAVA中的变量，自动类型转换</a><br><strong>强制类型转换</strong><br>将高级别赋值给低级别类型时，必须进行强制类型转换<br>1、 转换的数据类型必须是兼容的。<br>2、格式：(type)value type是要强制类型转换后的数据类型<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">public static void main(String[] args) &#123;</span><br><span class="line">	//强制类型转换</span><br><span class="line">	//高级别转低级别时，必须要进行强转</span><br><span class="line">	//是否强制转换取决于数据类型级别的高低，不取决于数据的实际值。</span><br><span class="line">	int a=330;</span><br><span class="line">	short b=(short)a; </span><br><span class="line">	short c=350;//直接自变量赋值 ，不需要转换类型</span><br><span class="line"></span><br><span class="line">	int e=(int)-1.1;</span><br><span class="line">	System.out.print(e); //e=-1</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h2 id="封装类"><a href="#封装类" class="headerlink" title="封装类"></a>封装类</h2><p>因为泛型类包括预定义的集合，使用的参数都是对象类型，无法直接使用基本数据类型，所以Java又提供了这些基本类型的封装类。<br>每一种基本数据类型都有与之相对应的封装类，也称包装类，如下表所示：</p>
<table>
<thead>
<tr>
<th>基本数据类型</th>
<th>对应封装类</th>
</tr>
</thead>
<tbody>
<tr>
<td>byte</td>
<td>Byte</td>
</tr>
<tr>
<td>short</td>
<td>Short</td>
</tr>
<tr>
<td>int</td>
<td>Integer</td>
</tr>
<tr>
<td>long</td>
<td>Long</td>
</tr>
<tr>
<td>float</td>
<td>Float</td>
</tr>
<tr>
<td>double</td>
<td>Double</td>
</tr>
<tr>
<td>char</td>
<td>Character</td>
</tr>
<tr>
<td>boolean</td>
<td>Boolean</td>
</tr>
</tbody>
</table>
<p>基本类型和对应的封装类由于本质的不同。具有一些区别：</p>
<ol>
<li>基本类型只能按值传递，而封装类按引用传递。</li>
<li>基本类型会在栈中创建，而对于对象类型，对象在堆中创建，对象的引用在栈中创建，基本类型由于在栈中，效率会比较高，但是可能存在内存泄漏的问题。</li>
</ol>
<h2 id="自动装箱和拆箱"><a href="#自动装箱和拆箱" class="headerlink" title="自动装箱和拆箱"></a>自动装箱和拆箱</h2><p>Java从jdk1.5开始引入自动装箱和拆箱,使得基本数据类型与引用类型之间相互转换变得简单。<br>自动装箱: java自动将原始类型转化为引用类型的过程，自动装箱时编译器会调用valueOf方法,将原始类型转化为对象类型。<br>自动拆箱: java自动将引用类型转化为原始类型的过程，自动拆箱时编译器会调用intValue(),doubleValue()这类的方法将对象转换成原始类型值。<br>自动装箱主要发生在两种情况：一种是赋值时，一种是方法调用时。<br>a.赋值<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">Integer a = 3; //自动装箱</span><br><span class="line">int b = a; //自动拆箱</span><br></pre></td></tr></table></figure></p>
<p>b.方法调用<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">public Integer query(Integer a)&#123;</span><br><span class="line">   return a;</span><br><span class="line">&#125;</span><br><span class="line">query(3); //自动装箱</span><br><span class="line">int result = query(3); //自动拆箱</span><br></pre></td></tr></table></figure></p>
<p>注意：在使用拆装箱操作的时候，需要注意判断对象不要为null，否者java.lang.NullPointerException </p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">Integer i = null; </span><br><span class="line">i+=200; </span><br><span class="line">System.out.println(“i:”+i);</span><br></pre></td></tr></table></figure>
<p>本文另参考了：<br><a href="http://www.runoob.com/java/java-basic-datatypes.html" target="_blank" rel="noopener">http://www.runoob.com/java/java-basic-datatypes.html</a><br><a href="https://blog.csdn.net/a909301740/article/details/52084853" target="_blank" rel="noopener">https://blog.csdn.net/a909301740/article/details/52084853</a><br><a href="https://blog.csdn.net/xuehyunyu/article/details/76216536" target="_blank" rel="noopener">https://blog.csdn.net/xuehyunyu/article/details/76216536</a></p>

            </div>
        
        <footer class="article-footer">
        </footer>
    </div>
</article>


    
<nav id="article-nav">
    
        <a href="/2018/08/12/Java运算符/" id="article-nav-newer" class="article-nav-link-wrap">
            <strong class="article-nav-caption">上一篇</strong>
            <div class="article-nav-title">
                
                    Java运算符
                
            </div>
        </a>
    
    
        <a href="/2018/08/12/Java安装/" id="article-nav-older" class="article-nav-link-wrap">
            <strong class="article-nav-caption">下一篇</strong>
            <div class="article-nav-title">Java环境安装</div>
        </a>
    
</nav>





    
    




<!-- baidu url auto push script -->
<script type="text/javascript">
    !function(){var e=/([http|https]:\/\/[a-zA-Z0-9\_\.]+\.baidu\.com)/gi,r=window.location.href,o=document.referrer;if(!e.test(r)){var n="//api.share.baidu.com/s.gif";o?(n+="?r="+encodeURIComponent(document.referrer),r&&(n+="&l="+r)):r&&(n+="?l="+r);var t=new Image;t.src=n}}(window);
</script>     
</section>
        </div>
        <footer id="footer">
    <div class="outer">
        <div id="footer-info" class="inner">
            luis &copy; 2018 
            <a rel="license" href="http://creativecommons.org/licenses/by-nc-nd/4.0/"><img alt="Creative Commons License" style="border-width:0" src="https://i.creativecommons.org/l/by-nc-nd/4.0/80x15.png" /></a>
            <br> Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>. Theme - <a href="https://github.com/zthxxx/hexo-theme-Wikitten">wikitten</a>
            
                <br>
                <span id="busuanzi_container_site_pv"><i class="fa fa-eye"></i> <span id="busuanzi_value_site_pv"></span></span>
                &nbsp;|&nbsp;
                <span id="busuanzi_container_site_pv"><i class="fa fa-user"></i> <span id="busuanzi_value_site_uv"></span></span>
            
        </div>
    </div>
</footer>

        

    
        <script src="/libs/lightgallery/js/lightgallery.min.js"></script>
        <script src="/libs/lightgallery/js/lg-thumbnail.min.js"></script>
        <script src="/libs/lightgallery/js/lg-pager.min.js"></script>
        <script src="/libs/lightgallery/js/lg-autoplay.min.js"></script>
        <script src="/libs/lightgallery/js/lg-fullscreen.min.js"></script>
        <script src="/libs/lightgallery/js/lg-zoom.min.js"></script>
        <script src="/libs/lightgallery/js/lg-hash.min.js"></script>
        <script src="/libs/lightgallery/js/lg-share.min.js"></script>
        <script src="/libs/lightgallery/js/lg-video.min.js"></script>
    
    
        <script src="/libs/justified-gallery/jquery.justifiedGallery.min.js"></script>
    
    
        <script type="text/x-mathjax-config">
    MathJax.Hub.Config({
        tex2jax: {
            inlineMath: [ ["$","$"], ["\\(","\\)"] ],
            skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code'],
            processEscapes: true,
            TeX: {
                equationNumbers: {
                  autoNumber: 'AMS'
                }
            }
        }
    });
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax();
        for (var i = 0; i < all.length; ++i)
            all[i].SourceElement().parentNode.className += ' has-jax';
    });
</script>
<script async src="//cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
    



<!-- Custom Scripts -->
<script src="/js/main.js"></script>

    </div>
</body>
</html>